﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace PositionMeasureSystem
{
    public partial class SensorSetting : UserControl
    {
        public SensorSetting ()
        {
            InitializeComponent();
            for (int i = 0; i < Form1.strSensorState.Count; i++)
            {
                if (Form1.strSelNode.Equals(Form1.strSensorState[i][0]))
                {
                    tb_MeasureName.Text = Form1.strSensorState[i][1];
                    cbox_MeasureType.Text = Form1.strSensorState[i][2];
                    tb_tolerance.Text = Convert.ToString(Form1.floatSensorState[i][3]);
                }
            }
        }
       
        //保存设置数据
        private void bt_save_Click (object sender, EventArgs e)
        {
            SaveSetting();
        }
        public void SaveSetting ()
        {
            for (int i = 0; i < Form1.strSensorState.Count; i++)
            {
                if (Form1.strSelNode.Equals(Form1.strSensorState[i][0]))
                {
                    Form1.strSensorState[i][1] = tb_MeasureName.Text;
                    Form1.strSensorState[i][2] = cbox_MeasureType.Text;
                    Form1.floatSensorState[i][3] = float.Parse(tb_tolerance.Text);
                }
            }
            ModifyXmlInformation(Form1.sensorSetXml);
        }
        //保存传感器设置数据至XML文件
        private void ModifyXmlInformation (string xmlFilepath)
        {
            try
            {
                int i = 0;
                XmlDocument myXmlDoc = new XmlDocument();
                myXmlDoc.Load(xmlFilepath);
                XmlNode rootNode = myXmlDoc.SelectSingleNode("SensorSetting");
                XmlNodeList xmlNodeList = rootNode.ChildNodes;
                xmlNodeList[3].InnerText = Form1.strProjectInfo[0];
                xmlNodeList[4].InnerText = Form1.strProjectInfo[1];
                xmlNodeList[5].InnerText = Form1.strProjectInfo[4];
                XmlNodeList xmlSensor = xmlNodeList[6].ChildNodes;
                foreach (XmlNode xmlNode in xmlSensor)
                {
                    XmlNodeList sensorStateXml = xmlNode.ChildNodes;
                    sensorStateXml.Item(2).InnerText = Form1.strSensorState[i][1];
                    sensorStateXml.Item(3).InnerText = Form1.strSensorState[i][2];
                    sensorStateXml.Item(4).InnerText = Convert.ToString(Form1.floatSensorState[i][1]);
                    sensorStateXml.Item(5).InnerText = Convert.ToString(Form1.floatSensorState[i][2]);
                    sensorStateXml.Item(6).InnerText = Convert.ToString(Form1.floatSensorState[i][3]);
                    i++;
                }

                myXmlDoc.Save(xmlFilepath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        //传感器置零
        private void bt_zero_Click (object sender, EventArgs e)
        {
            for (int i = 0; i < Form1.strSensorState.Count; i++)
            {
                if (Form1.strSelNode.Equals(Form1.strSensorState[i][0]))
                {

                    DataGridView dataGridView = (DataGridView)(((SplitContainer)(((SplitContainer)this.Parent.Parent.Parent.Parent).Panel1.Controls[0])).Panel2.Controls[0]);
                    Form1.floatSensorState[i][2] = float.Parse((string)dataGridView.Rows[i].Cells[1].Value);
                }
            }
            ModifyXmlInformation(Form1.sensorSetXml);
        }

        private void cbox_MeasureType_SelectedIndexChanged (object sender, EventArgs e)
        {
            for (int i = 0; i < Form1.strSensorState.Count; i++)
            {
                if (Form1.strSelNode.Equals(Form1.strSensorState[i][0]))
                {
                    Form1.strSensorState[i][2] = cbox_MeasureType.Text;
                }
            }
            ModifyXmlInformation(Form1.sensorSetXml);
        }

        private void tb_tolerance_TextChanged (object sender, EventArgs e)
        {
            for (int i = 0; i < Form1.strSensorState.Count; i++)
            {
                if (Form1.strSelNode.Equals(Form1.strSensorState[i][0]))
                {
                    Form1.floatSensorState[i][3] = float.Parse(tb_tolerance.Text);
                }
            }
            ModifyXmlInformation(Form1.sensorSetXml);
        }

        private void tb_MeasureName_TextChanged (object sender, EventArgs e)
        {
            for (int i = 0; i < Form1.strSensorState.Count; i++)
            {
                if (Form1.strSelNode.Equals(Form1.strSensorState[i][0]))
                {
                    Form1.strSensorState[i][1] = tb_MeasureName.Text;
                }
            }
            ModifyXmlInformation(Form1.sensorSetXml);
        }
    }
}
